import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/service/lf_auth_service.dart';

import '../../res/lf_fonts.dart';
import '../../res/lf_text_style.dart';
import '../../res/strings/lf_strings.dart';
import '../../widgets/lf_button.dart';
import '../lf_payment/lf_payment_logic.dart';

class LfSearchWaiting extends StatefulWidget {
  final Function timeOver;

  const LfSearchWaiting({
    Key? key,
    required this.timeOver,
  }) : super(key: key);

  @override
  State<LfSearchWaiting> createState() => _LfPickLimitState();
}

class _LfPickLimitState extends State<LfSearchWaiting> {
  LfAuthService authService = Get.find();
  RxInt duration = 0.obs;
  Timer? t;

  @override
  void initState() {
    duration.value = authService.limitEntity?.remainTime ?? 0;
    t = Timer.periodic(const Duration(seconds: 1), (timer) {
      duration--;
      if (duration.value <= 0) {
        t?.cancel();
        t = null;
        widget.timeOver.call();
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    t?.cancel();
    t = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SizedBox(
            width: 180.r,
            height: 180.r,
            child: Stack(
              children: [
                Positioned.fill(
                  child: Obx(() {
                    double value = (86400 - duration.value) / (86400);
                    return CircularProgressIndicator(
                      backgroundColor: colorFF007F,
                      valueColor:
                          AlwaysStoppedAnimation(Colors.grey.withAlpha(220)),
                      value: value,
                      strokeWidth: 10.r,
                    );
                  }),
                ),
                Center(child: Obx(() {
                  String hour = twoDigits(duration.value ~/ 3600);
                  String min = twoDigits((duration.value % 3600).toInt() ~/ 60);
                  String second =
                      twoDigits((duration.value % 3600).toInt() % 60);
                  return Text(
                    '$hour : $min : $second',
                    style: lfTextStyle(sp: 20.sp, fontFamily: fontBold),
                  );
                })),
              ],
            ),
          ),
          40.h.sbh,
          Text(
            strOutAround.tr,
            style: lfTextStyle(sp: 17.sp, fontFamily: fontMedium),
            textAlign: TextAlign.center,
          ),
          40.h.sbh,
          LfButton(
            text: strUpgrade.tr,
            onPress: () {
              toPayment();
            },
            enabled: true,
          )
        ],
      ),
    );
  }

  static String twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }
}
